package com.aliyun.frontframework;

import java.util.List;

import android.hardware.Camera;
import android.util.Log;

/**
 * Camera-related utility functions.
 */
public class CameraUtils {
    private static final String TAG = "CameraUtils DEADBEAF";

    /**
     * Attempts to find a preview size that matches the provided width and height (which
     * specify the dimensions of the encoded video).  If it fails to find a match it just
     * uses the default preview size for video.
     * <p>
     * TODO: should do a best-fit match, e.g.
     * https://github.com/commonsguy/cwac-camera/blob/master/camera/src/com/commonsware/cwac/camera/CameraUtils.java
     */
    public static void choosePreviewSize(Camera.Parameters parms, int width, int height) {
        // We should make sure that the requested MPEG size is less than the preferred
        // size, and has the same aspect ratio.
        Camera.Size ppsfv = parms.getPreferredPreviewSizeForVideo();
        if (ppsfv != null) {
            Log.d(TAG, "Camera preferred preview size for video is " +
                    ppsfv.width + "x" + ppsfv.height);
        }

        //for (Camera.Size size : parms.getSupportedPreviewSizes()) {
        //    Log.d(TAG, "supported: " + size.width + "x" + size.height);
        //}

        for (Camera.Size size : parms.getSupportedPreviewSizes()) {
            Log.d(TAG, "support size " + size.width + " " + size.height);
            if (size.width == width && size.height == height) {
                parms.setPreviewSize(width, height);
                return;
            }
        }

        int goodHeight= 0;
        int goodWidth = 0;
        int delta = 99999; //肯定比这个值要大啦//以小的边为基准。然后找最靠近的值
        
        if (height >= width) {
            for (Camera.Size size : parms.getSupportedPreviewSizes()) {
                Log.d(TAG, "support size " + size.width + " " + size.height);
                if (Math.abs(size.width - width) < delta) {
                    delta = Math.abs(size.width - width);
                    goodHeight = size.height;
                    goodWidth = size.width;
                }
            }
        } else {
            for (Camera.Size size : parms.getSupportedPreviewSizes()) {
                Log.d(TAG, "support size " + size.width + " " + size.height);
                if (Math.abs(size.height - height) < delta) {
                    delta = Math.abs(size.height - height);
                    goodHeight = size.height;
                    goodWidth = size.width;
                }
            }  
        }
        Log.w(TAG, "Changed to set preview size to " + goodWidth + "x" + goodHeight);
        parms.setPreviewSize(goodWidth, goodHeight);
//        Log.w(TAG, "Unable to set preview size to " + width + "x" + height);
//        if (ppsfv != null) {
//            parms.setPreviewSize(ppsfv.width, ppsfv.height);
//        }
        // else use whatever the default size is
    }

    /**
     * Attempts to find a fixed preview frame rate that matches the desired frame rate.
     * <p>
     * It doesn't seem like there's a great deal of flexibility here.
     * <p>
     * TODO: follow the recipe from http://stackoverflow.com/questions/22639336/#22645327
     *
     * @return The expected frame rate, in thousands of frames per second.
     */
    public static int chooseFixedPreviewFps(Camera.Parameters parms, int desiredThousandFps) {
        List<int[]> supported = parms.getSupportedPreviewFpsRange();

        for (int[] entry : supported) {
            //Log.d(TAG, "entry: " + entry[0] + " - " + entry[1]);
            if ((entry[0] == entry[1]) && (entry[0] == desiredThousandFps)) {
                parms.setPreviewFpsRange(entry[0], entry[1]);
                return entry[0];
            }
        }

        int[] tmp = new int[2];
        parms.getPreviewFpsRange(tmp);
        int guess;
        if (tmp[0] == tmp[1]) {
            guess = tmp[0];
        } else {
            guess = tmp[1] / 2;     // shrug
        }

        Log.d(TAG, "Couldn't find match for " + desiredThousandFps + ", using " + guess);
        return guess;
    }
}
